#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 #include <unistd.h>


#define BUFSIZE 32

int mul_copy(int argc, char *argv[]);

int main(int argc, char *argv[])
{
	if(argc < 2){
		return 1;
	}
	mul_copy(argc, argv);
	return 0;
}

int mul_copy(int argc, char *argv[])
{
	int fd1, fd2;
	int len;
	char buf[BUFSIZE + 1] = {};

	fd1 = open(argv[1],O_RDONLY);
	if(fd1 == -1){
		perror("open1()");
		return -1;
	}
	if(argc == 2){
		fd2 = 1; 
	}else if(argc > 2){
		fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0664); 
		if(fd2 == -1){
			close(fd1);
			perror("open2()");
			return -1;
		}
	}

	while(1){
		if((len = read(fd1, buf, BUFSIZE)) == 0){
			break;
		}
		write(fd2, buf, len);
	}
	close(fd1);
	close(fd2);
	return 0;
}
